<template>
  <!--
        :on-preview="handlePictureCardPreview"
        :on-remove="handleRemove"
    -->
  <div class="upLoad">
    <el-upload
      :class="{disable:isDisable}"
      action="#"
      list-type="picture-card"
      :file-list="fileList"
      :on-change="handleChange"
      :on-remove="handleRemove"
      :on-preview="handlePictureCardPreview"
      :limit="limit"
      :http-request="upload"
      :before-upload="beforeAvatarUpload"
    >
      <i class="el-icon-plus" />
    </el-upload>

    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <!-- 进度条组件 -->
    <div v-if="showProgress">
      <div>上传进度：<el-progress style="width: 180px;" :percentage="percent" /></div>
    </div>
  </div>
</template>
<script>
import COS from 'cos-js-sdk-v5' // 导入腾讯云的包(sdk)
const cos = new COS({
  SecretId: 'AKIDlbRs3lGwQeaIMDnq0vuooY3HETTDKSGo', // 身份识别ID
  SecretKey: 'ixEQ4TbpqiEg4C31FG2mV3OTO7g5JLTL' // 身份秘钥
})
export default {
  name: 'ImageUpload',
  props: {
    limit: {
      type: Number,
      default: 2
    }
  },
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      fileList: [
        // { name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100' },
        // { name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100' }
      ],
      showProgress: false,
      percent: 0

    }
  },
  computed: {
    isDisable() {
      // 当照片的数量超过所限制的数量，隐藏加号
      return this.fileList.length >= this.limit
    },
    isAllSuccess() {
      return this.fileList.some(item => item.status !== 'success')
    }

  },
  methods: {
    handleRemove(file, fileList) {
      this.fileList = this.fileList.filter(item => item.uid !== file.uid)
    //   this.fileList = []
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    // 进行上传操作
    upload(params) {
    //   console.log('上传', params.file)
      const fileObj = this.fileList.find(item => item.uid === params.file.uid)
      fileObj.status = 'uploading'
      if (params.file) {
        this.showProgress = true
        // 执行上传操作
        cos.putObject({
          Bucket: 'jepson-75-1256203106', /* 存储桶 */
          Region: 'ap-shanghai', /* 存储桶所在地域，必须字段 */
          Key: params.file.name, /* 文件名 */
          StorageClass: 'STANDARD', // 上传模式, 标准模式
          Body: params.file, // 上传文件对象
          onProgress: (progressData) => {
            console.log('progressData', JSON.stringify(progressData))
            console.log(progressData)
            this.percent = progressData.percent * 100
            fileObj.percentage = progressData.percent * 100
            setTimeout(() => {
              this.showProgress = false
              this.percent = 0
            }, 300)
          }
        }, (err, data) => {
          console.log(err || data)
          if (data) {
            setTimeout(() => {
              fileObj.status = 'success'
            }, 500)
            this.fileList.find(item => item.uid === params.file.uid).url = 'http://' + data.Location
          } else {
            console.log(err)
          }
        })
      }
    },
    // 添加文件, 用户选了就应该新增文件预览
    // 文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用
    handleChange(file, fileList) {
    //   console.log(fileList)
      // console.log(fileList)
      // console.log(fileList.length)
      this.fileList = [...fileList]
    },
    beforeAvatarUpload(file) {
      const isJPG = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png'].includes(file.type)
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      //   console.log(isJPG, isLt2M)
      return isJPG && isLt2M
    }
  }
}
</script>

<style scoped lang="scss">
    .disable{
        ::v-deep{
    .el-upload--picture-card{
            display: none;
        }
    }
    }
</style>
